Ext.onReady(function() {
	var departmentStore = Ext.create('Ext.data.Store', {
    	fields: ["id", "departmentName"],
    	remoteSort: false,
    	proxy: {
            type: 'ajax',
            url: contextPath+'/system/user/findAllDept',
            actionMethods: { read: 'POST' },
            reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
                type: 'json', //返回数据类型为json格式
                root: 'obj'
            }
    	},
    	autoLoad: true  //即时加载数据
	});
	var departmentUserStore = Ext.create('Ext.data.Store', {
    	fields: ["id", "userName"],
    	proxy: {
            type: 'ajax',
            url: contextPath+'/system/user/findUserByDeptId',
            actionMethods: { read: 'POST' },
            reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
                type: 'json', //返回数据类型为json格式
                root: 'obj'
            }
    	},
    	autoLoad: false
	});
	var callTimeStoreList = [
	                       		{"value":"1","text":"自定"},
	                        	{"value":"0","text":"全部"}
	                       	];
	var callTimeStore = Ext.data.Store({
	                           	   autoDestroy: true,
	                               fields :["value","text"],
	                               data : callTimeStoreList
	 });
	
	var callStatusStoreList = [
                                {"value":"","text":"不限"},
	                       		{"value":"1","text":"接通"},
	                        	{"value":"2","text":"未接通"}
	                       	];
	var callStatusStore = Ext.data.Store({
	                           	   autoDestroy: true,
	                               fields :["value","text"],
	                               data : callStatusStoreList
	 });
	
	var terminalModelStoreList = [
                                {"value":"","text":"不限"},
	                       		{"value":"0","text":"客户"},
	                        	{"value":"1","text":"外部呼入"},
	                        	{"value":"2","text":"手动拨打"}
	                       	];
	var terminalModelStore = Ext.data.Store({
	                           	   autoDestroy: true,
	                               fields :["value","text"],
	                               data : terminalModelStoreList
	 });
    var column_1={
    		items:[{
    			xtype:'container',
            	layout:'hbox',
            	items:[{
    	            xtype:'combobox',  
    	            displayField: "text",           
    	            valueField: "value",  
    	            fieldLabel:'日期',
    	            store: callTimeStore,
    	            typeAhead: true,
    	            width:160,
    	            value:'1',
    	            listeners:{
    	            	'change':function(value){
    	            		var selectValue=value.lastValue;
    	            		if(selectValue=="0"){
    	            			Ext.getCmp("callTimeBefore").reset();
    	            			Ext.getCmp("callTimeAfter").reset();
    	            		}
    	            	}
    	            }
    	            
                },{
                	margin: "0 0 0 5",
            		xtype:'datefield',  
                    format:'Y-m-d',
                    hideLabel:true,
                    name:'callTimeBeforeStr',
                    id:'callTimeBefore',
                    width:80,
            	},{
            		margin: "0 0 0 5",
            		xtype:'datefield',  
                    format:'Y-m-d',
                    width:80,
                    name:'callTimeAfterStr',
                    id:'callTimeAfter',
                    hideLabel:true
            	}]
                
            },{
            	margin: "5 0 0 0",
	            xtype:'combobox',  
	            displayField: "text",           
	            valueField: "value",  
	            fieldLabel:'状态',
	            store: callStatusStore,
	            typeAhead: true,
	            value:"",
	            name:"callStatus"
            }]	
	};
    var column_2={
    		items:[{
    			xtype:'container',
            	layout:'hbox',
            	items:[{
    	            xtype:'combobox',  
    	            displayField: "departmentName",           
    	            valueField: "id",  
    	            fieldLabel:'通话人',
    	            store: departmentStore,
    	            typeAhead: true,
    	            width:220,
                    name:"userDepartmentId",
                    queryMode : 'local',
    	            listeners:{
    	            	'change':function(value){
    	            		Ext.getCmp("createUserIdCombobox").reset();
    	            		var selectValue=value.lastValue;
    	            		departmentUserStore.load({
    	            			params:{
    	            				'deptId':value.lastValue
    	            			}
    	            		});
    	            	}
    	            }
    	            
                },{
                	margin: "0 0 0 5",
    	            xtype:'combobox',  
    	            displayField: "userName",           
    	            valueField: "id",  
    	            store: departmentUserStore,
    	            queryMode : 'local',
    	            typeAhead: true,
    	            width:100,
    	            hideLabel:true,
                    name:"createUserId",
    	            id:'createUserIdCombobox'
                }]
                
            },{
            	margin: "5 0 0 0",
            	xtype:'container',
            	layout:'hbox',
            	items:[{
                    xtype:'numberfield',  
                    fieldLabel:"时长",
                    hideTrigger:true,
                    width:220,
                    name:"startBillableSeconds"
                    
            	},{
            		margin: "0 5 0 5",
                	xtype: 'displayfield',
                    value:'秒-',
                    hideLabel:true,
                    width:8
                },{
            		margin: "0 0 0 5",
                    xtype:'numberfield', 
                    width:100,
                    hideLabel:true,
                    hideTrigger:true,
                    name:"endBillableSeconds"
            	},{
            		margin: "0 5 0 5",
                	xtype: 'displayfield',
                    value:'秒',
                    hideLabel:true,
                    width:8
                }]
            	
            }]	
	};
    var column_3={
    		items:[{
	            xtype:'combobox',  
	            displayField: "text",           
	            valueField: "value",  
	            fieldLabel:'来源',
	            store: terminalModelStore,
	            typeAhead: true,
	            name:"terminalModel",
	            value:""
            },{
            	anchor: '90%',
            	margin: "5 0 0 0",
                xtype:'textfield',  
                fieldLabel: "关键词",
                name:"customerName",
                emptyText:'客户姓名/主叫号码/被叫号码' 
            }]	
	};
	var items=[column_1,column_2,column_3];
	var searchForm=Ext.create("Ext.form.Panel",{
		region:'north',
		title:"查询条件",
        collapsible: true,  //可以折叠,
        items:[{
        	xtype: 'container',
        	margin: 10,
            layout: 'hbox',
            fieldDefaults: {
                labelAlign: 'left',
                msgTarget: 'side',
                anchor: '80%',
                labelWidth:120
            },
            defaults: {
                border: false,
                xtype: 'container',
                flex: 1,
                layout: 'anchor'
                
            },
            items:items
        }],
        buttons: [/*{
        	xtype:"displayfield",
        	value: '注:系统将定时获取通话情况。如显示"未取话单"，请等待系统获取通话情况。'
        },*/{
        	align:'right',
        	xtype: "button", text: "查询", handler:function(){
        		    var pagingTool=Ext.getCmp("pagingtoolbarId");
	        		pagingTool.moveFirst();//跳至第一页
        	}
        },{
        	align:'right',
        	xtype: "button", text: "清空", handler:function(){
        		searchForm.getForm().reset();
        	}
        }]
	});
	
	
	var gridStoresp = Ext.create('Ext.data.Store', {
    	fields: ["id", "userName"
             , "customerName","departmentName","callTime"
             ,"billableSeconds","called","caller","callType","terminalType",
             "callStatus","terminalModel","terminalModelId","recordUrl","startTime","endTime","projectId"],
    	pageSize: 20,  //页容量5条数据
    	//是否在服务端排序 （true的话，在客户端就不能排序）
    	remoteSort: false,
    	proxy: {
            type: 'ajax',
            url: contextPath+'/system/telRecord/list',
            actionMethods: { read: 'POST' },
            reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
                type: 'json', //返回数据类型为json格式
                root: 'obj.rows',  //数据
                totalProperty: 'obj.total' //数据总条数
            }
    	},
    	autoLoad: true  //即时加载数据
	});
	
	gridStoresp.on("beforeload",function(){
    	Ext.apply(gridStoresp.proxy.extraParams,searchForm.getForm().getValues());
	});
	
	var columns=[
	              /*{text:'路径',width:'12%',renderer : function(value,cellmeta,record,rowIndex,columnIndex,store){
	            	  //通话类型:0：呼出；1：呼入
	            	  var callType=record.get("callType");
	            	  //终端类型:0：PC；1：手机；3:座机
	            	  var terminalType=record.get("terminalType");
	            	  //通话状态:0：待获取；1：接通；2未接通
	            	  var callStatus=record.get("callStatus");
	            	  var result="";
	            	  if(callType=="0"){
	            		  result=result+"呼出";
	            	  }else if(callType=="1"){
	            		  result=result+"呼入";
	            	  }
	            	  if(terminalType=="0"){
	            		  result=result+"-PC";
	            	  }else if(terminalType=="1"){
	            		  result=result+"-手机";
	            	  }else if(terminalType=="2"){
	            		  result=result+"-座机";
	            	  }
	            	  if(callStatus=="0"){
	            		  result=result+"-未取话单";
	            	  }else if(callStatus=="1"){
	            		  result=result+"-接通";
	            	  }else if(callStatus=="2"){
	            		  result=result+"-未接通";
	            	  }
	            	  return result;
	              }},*/
		          {text:'日期',dataIndex:'callTime',width:'8%',renderer : function(value,cellmeta,record,rowIndex,columnIndex,store){
		        	  if(null!=value && value!=""){
		        		  return Ext.Date.format(new Date(value),'Y-m-d');
		        	  }
		        	  return "";
		        	  
		          }},
		          {text:'开始时间',dataIndex:'startTime',width:'8%',renderer : function(value,cellmeta,record,rowIndex,columnIndex,store){
		        	  if(null!=value && value!=""){
		        		  return Ext.Date.format(new Date(value),'H:i:s'); 
		        	  }
		        	  return "";
		          }},
		          {text:'结束时间',dataIndex:'endTime',width:'8%',renderer : function(value,cellmeta,record,rowIndex,columnIndex,store){
		        	  if(null!=value && value!=""){
		        		  return Ext.Date.format(new Date(value),'H:i:s'); 
		        	  }
		        	  return "";
		          }},
		          {text:'时长',dataIndex:'billableSeconds',width:'8%',renderer : function(s,cellmeta,record,rowIndex,columnIndex,store){
		        	  var t="";
		        	  var callStatus=record.get("callStatus");  
		        	  if(callStatus=="0"){
	            		  t=t+"未取话单";
	            	  }else if(callStatus=="1"){
	            		  if(null!=s && s!=""){
			        	    	var min = Math.floor(s/60);
				        	    var sec = s % 60;
				        	    if(min < 10){t += "0";}
				        	        t += min + ":";
				        	    if(sec < 10){t += "0";}
				        	        t += sec;
			        	   }
	            	  }else if(callStatus=="2"){
	            		  t=t+"未接通";
	            	  }
		        	  return t;
		          }},
		          {text:'部门',dataIndex:'departmentName',width:'8%'},
		          {text:'员工',dataIndex:'userName',width:'8%'},
		          {text:'主叫号码',dataIndex:'caller',width:'8%'},
		          {text:'被叫号码',dataIndex:'called',width:'8%'},
		          {text:'来源',dataIndex:'terminalModel',width:'8%',renderer : function(value,cellmeta,record,rowIndex,columnIndex,store){
		        	  if(null!=value && value!=""){
		        		  if(value=="0"){
		        			  return "客户";
		        		  }
		        	  }
		        	  return "";
		          }},
		          {text:'关联业务',dataIndex:'customerName',width:'8%',renderer : function(value,cellmeta,record,rowIndex,columnIndex,store){
		        	  if(value && value!='null'){
		        		  var terminalModelId=record.get("terminalModelId");
			        	  var projectId=record.get("projectId");
			        	  return '<a target=\'_blank\' href="javascript:detail('+terminalModelId+','+projectId+')">'+value+'</a>';
		        	  }
		        	 
		          }},
		          {text:'操作',dataIndex:'combinedName',width:'20%',renderer : function(value,cellmeta,record,rowIndex,columnIndex,store) {
		        	  var recordUrl = record.get("recordUrl");
		        	  var html="";
		        	  if(null!=recordUrl && recordUrl!=""){
		        		  html+='<a href="javascript:previewAudio(\''+recordUrl+'\')">听录音</a>&nbsp;&nbsp;';
		            	  var fileName = recordUrl.substring(recordUrl.lastIndexOf('/') + 1);
		            	  html+= '<a href="'+contextPath+'/common/file/downloadFile?fileUrl='+recordUrl+'&fileName='+fileName+'">下载</a>';
		        	  }
		        	  
		        	  return html;
		          }}
	            ];
	
	var gridsp = Ext.create('Ext.grid.Panel', {
		region:'center',
		title: '通话记录列表&nbsp;&nbsp;&nbsp;(注:系统将定时获取通话情况。如显示"未取话单"，请等待系统获取通话情况。)',
		collapsible: true,
        store: gridStoresp,
        columnLines: true,
        columns: columns,
        dockedItems: [{
            xtype: 'pagingtoolbar',  
            store: gridStoresp,   // GridPanel中使用的数据  
            dock: 'bottom',  
            displayInfo: true,  
            emptyMsg:"没有数据",  
            displayMsg:"显示从{0}条数据到{1}条数据，共{2}条数据",
            id:'pagingtoolbarId'
        }],
    });
	
	Ext.create("Ext.form.Panel",{
		renderTo:Ext.getBody(), 
		items:[searchForm,gridsp]
	});
	
/*	var viewport = Ext.create('Ext.Viewport', {
		layout:'border',
		items:[searchForm,gridsp]
	});*/
	

});

function detail(id,projectId){
	var openUrl=contextPath + "/customer/customer/toDetail?customerId="+id+"&projectId="+projectId+"&resourcesCode=menu_0003_0001";
	window.open(openUrl);
}

function previewAudio(url){
	var html='<audio controls="controls"> <source src="'+url+'" type="audio/mpeg"> </audio>';
	var window = Ext.create('Ext.Window', {
        title: "播放录音",
        width: '300',
        height: '80',
        modal : true, 
        //autoScroll:true,
        //maximizable:false,
        layout : 'fit', 
        html:html
    });
	window.show();
}


